package example.service.impl;

import cn.hutool.core.lang.UUID;
import example.common.Result;
import example.dto.TokenDTO;
import example.entity.Department;
import example.mapper.DepartmentMapper;
import example.mapper.LevelMapper;
import example.mapper.LimitMapper;
import example.service.IDepartmentService;
import example.vo.DepartmentInfoVO;
import example.vo.Menu;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class DepartmentServiceImpl implements IDepartmentService {

    @Resource
    private DepartmentMapper departmentMapper;

    @Resource
    private LevelMapper levelMapper;

    @Resource
    private LimitMapper limitMapper;

    @Override
    public Result getDepartmentList() {
        List<Department> departmentList = departmentMapper.listDepartment();
        return Result.ok(departmentList);
    }

    @Override
    public Result getDepartmentDetailToUpdate(String departmentName) {
        Department department = departmentMapper.getDepartmentByName(departmentName);
        return Result.ok(department);
    }

    @Override
    public Result updateDepartmentDetail(Department department) {
       departmentMapper.updateDepartmentDetail(department);
        return Result.ok("修改成功");
    }

    @Override
    public Result addDepartment(Department department) {
        String levelName = levelMapper.getLevelNameById(department.getLevelId());
        department.setLevelName(levelName);
        departmentMapper.save(department);
        return Result.ok("新增部门成功！");
    }

    @Override
    public Result deleteDepartment(Long departmentId) {
        List<Long> cId = new ArrayList<>();
        cId.add(departmentId);
        List<Department> departments = departmentMapper.listDepartment();
        for (Department department : departments) {
            if (department.getPid() == departmentId) {
                cId.add(department.getId());
            }
        }
        for (Long id : cId) {
            departmentMapper.deleteDepartment(id);
        }
        return Result.ok("删除部门成功！");
    }

    @Override
    public Result listDepartmentNames() {
        List<String> names = departmentMapper.listDepartmentNames();
        return Result.ok(names);
    }

    @Override
    public Result getDepartmentInfo(String departmentName) {
        String token = UUID.randomUUID().toString(true);
        TokenDTO tokenDTO = new TokenDTO();
        tokenDTO.setDepartmentName(departmentName);
        tokenDTO.setToken(token);
        departmentMapper.saveTokenByName(tokenDTO);
        return Result.ok(token);
    }

    @Override
    public Result verifyDepartmentInfo(Department department) {
        int exist = departmentMapper.verifyDepartmentPassword(department);
        if (exist>0){
            return getDepartmentInfo(department.getName());
        }
       return Result.fail("密码错误");
    }

    @Override
    public Result getUserInfo(String token) {
        // Bearer b0726688955
        String[] s = token.split(" ");
        String str = s[s.length - 1];
        Department department = departmentMapper.getByToken(str);
        if (department==null){
            return Result.fail("请清除网站cookie重新登录");
        }
        Long levelId = department.getLevelId();
        List<String> menus = limitMapper.getLimits(levelId);
        DepartmentInfoVO departmentInfoVO = new DepartmentInfoVO();
        BeanUtils.copyProperties(department, departmentInfoVO);
        Menu menu = new Menu();
        menu.setMenus(menus);
        departmentInfoVO.setRoles(menu);
        return Result.ok(departmentInfoVO);
    }

    @Override
    public Result listDepNames(Long departmentId) {
        List<String> depNames = departmentMapper.lisNames(departmentId);
        return Result.ok(depNames);
    }
}
